#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "adduserdialog.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("用户管理系统");

    // 初始化用户列表，添加管理员用户
    users.append(qMakePair(QString("admin"), QString("8")));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionAddUser_triggered()
{
    AddUserDialog dialog(this);
    if (dialog.exec() == QDialog::Accepted) {
        QString username = dialog.getUsername();
        QString password = dialog.getPassword();

        // 检查用户名是否已存在
        for (const auto &user : users) {
            if (user.first == username) {
                QMessageBox::warning(this, "添加失败", "用户名已存在！");
                return;
            }
        }

        // 添加新用户
        users.append(qMakePair(username, password));
        QMessageBox::information(this, "添加成功", "用户 " + username + " 添加成功！");
    }
}
